import { axiosInstance } from 'src/boot/axios'
import CommonConfig from 'src/assets/js/config'
const baseUrlTpm = CommonConfig.tpmUrl + '/v1'
// import qs from 'qs'
// const qsConfig = {
//   arrayFormat: 'repeat',
//   skipNulls: true,
//   format: 'RFC3986'
// }

// GET
// /v1/maintenance_order/records
// 保养记录
export function getMaintenaceRecordsList ({ pageNum, pageSize,
  deviceCode, deviceType, endDate, keyType, keyWord, maintenanceType, programId,
  sort, workshopId, startDate }) {
  const newUrl = `${baseUrlTpm}/maintenance_order/records`
  const params = {
    pageNum,
    pageSize,
    deviceCode,
    deviceType: deviceType === 'all' ? null : deviceType,
    endDate,
    keyType,
    keyWord,
    maintenanceType: maintenanceType === 'all' ? null : maintenanceType,
    sort,
    workshopId: workshopId === 'all' ? null : workshopId,
    programId: programId === 'all' ? null : programId,
    startDate
  }
  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve((res))
  })
}
// /v1/maintenance_orders
// 保养工单列表
export function getMaintenaceOrdersList ({ pageNum, pageSize,
  deviceCode, deviceType, endDate, maintenanceStatus, keyType, keyWord,
  sort, workshopId, startDate, maintenanceCycle }) {
  const newUrl = `${baseUrlTpm}/maintenance_orders`
  const params = {
    pageNum,
    pageSize,
    deviceCode,
    keyType,
    keyWord,
    deviceType: deviceType === 'all' ? null : deviceType,
    endDate,
    maintenanceStatus: maintenanceStatus === 'all' ? null : maintenanceStatus,
    sort,
    workshopId: workshopId === 'all' ? null : workshopId,
    startDate,
    recurRuleFreq: maintenanceCycle === 'all' ? null : maintenanceCycle,
    type: 'pc'
  }
  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve((res))
  })
}
// GET
// /v1/maintenance_order/{id}
// 根据ID查询保养详情
export function getMaintenaceOrderById (id) {
  const newUrl = `${baseUrlTpm}/maintenance_order/${id}`
  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve((res))
  })
}
export function getMaintenancePrograms () {
  const newUrl = `${baseUrlTpm}/maintenance_programs`
  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve((res))
  })
}
// GET
// /v1/maintenance/record/export
// 导出保养记录
export function getExportRecord ({ pageNum, pageSize,
  deviceCode, deviceType, endDate, maintenanceType, keyType, keyWord, operator, requestId,
  sort, workshopId, startDate }) {
  const newUrl = `${baseUrlTpm}/maintenance/record/export`
  const params = {
    pageNum,
    pageSize,
    deviceCode,
    deviceType: deviceType === 'all' ? null : deviceType,
    endDate,
    requestId,
    keyType,
    keyWord,
    operator,
    maintenanceType: maintenanceType === 'all' ? null : maintenanceType,
    sort,
    workshopId: workshopId === 'all' ? null : workshopId,
    startDate
  }
  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve((res))
  })
}
// POST
// /v1/maintenance_order/start_maintenance
// 开始保养
export function postStartMaintenance (params) {
  const newUrl = `${baseUrlTpm}/maintenance_order/start_maintenance`
  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}
// POST
// /v1/maintenance_order/close
// 关闭保养工单
export function postCloseMaintenance (params) {
  const newUrl = `${baseUrlTpm}/maintenance_order/close`
  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}
// POST
// /v1/maintenance_order/complete_maintenance
// 保养结束
export function postCompleteMaintenance (params) {
  const newUrl = `${baseUrlTpm}/maintenance_order/complete_maintenance`
  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}
// POST
// /v1/maintenance_order/change_operator
// 保养工单转移
export function postChangeMaintenance ({ orderId, newOperatorIds }) {
  const newUrl = `${baseUrlTpm}/maintenance_order/change_operator?orderId=${orderId}&newOperatorIds=${newOperatorIds}`
  return axiosInstance.post(newUrl).then((res) => {
    return Promise.resolve((res))
  })
}

// 工单补录
export function supplementMaintenance (data) {
  const newUrl = `${baseUrlTpm}/maintenance_order/supplement_maintenance`
  return axiosInstance.post(newUrl, data).then((res) => {
    return Promise.resolve((res))
  })
}
